<?php
/**
 * Created by PhpStorm.
 * User: alexey
 * Date: 4/21/14
 * Time: 12:34 PM
 */
include_once("functions/db_manipulate.php");

if (!init()) {
  redirect("?action=login");
  exit();
}

include_once("classes/PHPExcel.php");


$lastVisits = getLastVisits();
$objPHPExcel = new PHPExcel();

$timeStamp = getLocalTimeStamp();

$page = $objPHPExcel->setActiveSheetIndex(0);

$objPHPExcel->getProperties()->setCreator("TheFlyNet.com");
$objPHPExcel->getProperties()->setLastModifiedBy("theflynet.com forum");
$objPHPExcel->getProperties()->setTitle("Last visits on forum theFlyNet.com");
$objPHPExcel->getProperties()->setSubject("Last visits on forum theFlyNet.com");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");

$i = 1;
$page->setCellValueByColumnAndRow(0, $i, "id");
$page->setCellValueByColumnAndRow(1, $i, "nick");
$page->setCellValueByColumnAndRow(2, $i, "date");
$page->setCellValueByColumnAndRow(3, $i, "time");
$i++;

while ($visit = mysql_fetch_assoc($lastVisits)) {
  $page->setCellValueByColumnAndRow(0, $i, $visit['vid']);
  $page->setCellValueByColumnAndRow(1, $i, $visit['nick']);
  $page->setCellValueByColumnAndRow(2, $i, $visit['visitdate']);
  $page->setCellValueByColumnAndRow(3, $i, $visit['visittime']);
  $i++;
}

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("../tmp/exportvisits.xlsx");

$file = basename("exportvisits.xlsx");
$file = "../tmp/".$file;

if(!$file){
  die('file not found');
} else {
  header("Cache-Control: public");
  header("Content-Description: File Transfer");
  header("Content-Disposition: attachment; filename=lastvisitors".f_Date($timeStamp).".xlsx");
  header("Content-Type: application/zip");
  header("Content-Transfer-Encoding: binary");
  readfile($file);
}

unlink($file);